package com.tianbao54.grpc.client;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import com.tianbao54.grpc.*;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.stub.StreamObserver;

import java.time.LocalDateTime;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;

public class StudentClient {

    public static void main(String[] args) {

        ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost",8888).usePlaintext(true).build();
        // 同步的 stub
        StudentServiceGrpc.StudentServiceBlockingStub blockingStub = StudentServiceGrpc.newBlockingStub(channel);
        // 异步的 stub
        StudentServiceGrpc.StudentServiceStub studentServiceStub = StudentServiceGrpc.newStub(channel);

        MyResponse response = blockingStub.getRealNameByUsername(MyRequest.newBuilder().setUsername("wangwu").build());
        System.out.println(response.getRealname());

        System.out.println("----------------------------------");

        Iterator<StudentResponse> iter = blockingStub.getStudentByAge(StudentRequest.newBuilder().setAge(100).build());
        while (iter.hasNext()) {
            System.out.println(iter.next());
        }

        System.out.println("----------------------------------");

        StreamObserver<StudentResponseList> studentResponseListStreamObserver = new StreamObserver<StudentResponseList>() {
            @Override
            public void onNext(StudentResponseList value) {
                value.getStudentResponseList().forEach(studentResponse -> {
                    System.out.println("response = " + studentResponse);
                });
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t.getMessage());
            }

            @Override
            public void onCompleted() {
                System.out.println("on Completed");
            }
        };
        // 客户端以流式发送给服务请求，那么请求一定是异步的。
        StreamObserver<StudentRequest> studentRequestStreamObserver = studentServiceStub
                .getStudentsWrapperByAges(studentResponseListStreamObserver);
        studentRequestStreamObserver.onNext(StudentRequest.newBuilder().setAge(300).build());
        studentRequestStreamObserver.onNext(StudentRequest.newBuilder().setAge(400).build());
        studentRequestStreamObserver.onNext(StudentRequest.newBuilder().setAge(500).build());
        studentRequestStreamObserver.onCompleted();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("---------------------------");
        StreamObserver<StreamRequest> requestStreamObserver = studentServiceStub.biTalk(new StreamObserver<StreamResponse>() {
            @Override
            public void onNext(StreamResponse value) {
                System.out.println("value = " + value.getResponseInfo());
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t.getMessage());
            }

            @Override
            public void onCompleted() {

                System.out.println("onCompleted");
            }
        });

        for (int i = 0 ; i < 10 ; i ++) {
            requestStreamObserver.onNext(StreamRequest.newBuilder().setRequestInfo(LocalDateTime.now().toString()).build());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
